<?php

	class CachedBehavior extends ModelBehavior {
		public function setup(&$model, $config = array()) {
			if (is_string($config)) {
				$config = array($config);
			}

	        $this->settings[$model->alias] = $config;
		}

		public function afterSave(&$model, $created) {
			$this->_deleteCachedFiles($model);
		}

	    public function afterDelete(&$model) {
			$this->_deleteCachedFiles($model);
		}

		protected function _deleteCachedFiles(&$model) {
			foreach ($this->settings[$model->alias]['prefix'] AS $prefix) {
				$files = glob(TMP.'cache'.DS.'queries'.DS.'cake_'.$prefix.'*');
				if (is_array($files) && count($files) > 0) {
					foreach ($files AS $file) {
						unlink($file);
					}
				}
			}
		}
	}
